package com.xneure.utils.cipher;

/**
 * 用户密码加密处理工具
 *
 * String zz = getKey(); String pwd = "admin"; String mPwd = getPassword(pwd,
 * zz); boolean v = verifyPassword(pwd, mPwd, zz);
 * 
 */
public class Password {
	static final int SALT_SIZE = 8;
	static final String HASH_ALGORITHM = "SHA-1";
	static final int HASH_INTERATIONS = 1024;

	public static String getKey() {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		return SimpleEncode.encodeHex(salt);
	}

	public static String getPassword(String pwd, String salt) {
		byte[] hashPassword = Digests.sha1(pwd.getBytes(), SimpleEncode.decodeHex(salt), HASH_INTERATIONS);
		return SimpleEncode.encodeHex(hashPassword);
	}

	public static boolean verifyPassword(String password, String secretPwd, String salt) {
		byte[] hashPassword = Digests.sha1(password.getBytes(), SimpleEncode.decodeHex(salt), HASH_INTERATIONS);
		String encrypt = SimpleEncode.encodeHex(hashPassword);
		return encrypt.equals(secretPwd);
	}

}
